//
//  ItemUI.m
//  mbstore
//
//  Created by huangyi on 14-3-12.
//  Copyright (c) 2014年 huangyi. All rights reserved.
//

#import "ItemUI.h"
#import "SuitUIV.h"
#import "ProductUIV.h"
#import "InfoUIM.h"
#define ORANGE [UIColor colorWithRed:1 green:100.0/255.0 blue:38.0/255.0 alpha:1]
@interface ItemUI ()


@end

@implementation ItemUI

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     responseCtr=[[MBStoreSrv alloc]init];
    // Do any additional setup after loading the view from its nib.

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
       
        
        NSArray *items=@[@"模特搭配",@"自助搭配"];
        UISegmentedControl *seg=[[UISegmentedControl alloc] initWithItems:items];
        seg.frame=CGRectMake(0, 0, 200, seg.frame.size.height);
        seg.tintColor=ORANGE;
        self.navigationItem.titleView=seg;
        seg.selectedSegmentIndex=0;
         NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:ORANGE,NSForegroundColorAttributeName,  [UIFont fontWithName:@"Helvetica" size:16.f],NSFontAttributeName ,[UIColor whiteColor],UITextAttributeTextShadowColor ,nil];
        [seg setTitleTextAttributes:dic forState:UIControlStateNormal];
        [seg addTarget:self action:@selector(onSegmentClick:) forControlEvents:UIControlEventValueChanged];
        UIButton *back=[UIButton buttonWithImage:@"back.png"];
        [back addTarget:self action:@selector(OnBack:) forControlEvents:UIControlEventTouchUpInside];
        NSMutableArray *leftItem=[NSMutableArray arrayWithObject:back.barButtonItem];
        [leftItem addObjectsFromArray:self.navigationController.navigationItem.leftBarButtonItems];
        self.navigationItem.leftBarButtonItems=leftItem;
        self.navigationItem.rightBarButtonItems=self.navigationController.navigationItem.rightBarButtonItems;
    });
}
-(void)OnBack:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)ShowTree:(UIView *)subview{
    [self AddChildWithNavigationController:subview];
   

}
-(void)onSegmentClick:(id)sender{
    CATransition *animation = [CATransition animation];
    animation.duration = 0.25;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionFade;
    [self.view.layer addAnimation:animation forKey:@"animation"];

    UISegmentedControl *seg=sender;
    [self.view removeAllSubviews];
    if (seg.selectedSegmentIndex==0) {
        SuitUIV *suit=(id)[NibLoader loadNib:@"SuitUIV"];
        suit.frame=self.view.bounds;
        suit.delegate=self;
        [self.view addSubview:suit];
    }
    if (seg.selectedSegmentIndex==1) {
        ProductUIV *product=(id)[NibLoader loadNib:@"ProductUIV"];
        product.frame=self.view.bounds;
        product.delegate=self;
        [self.view addSubview:product];
    }
//    NSLog(@"%@",self.view.subviews);
}

-(void)setSuitIdx:(int)suitIdx{
    MainApp *app=[MainApp mainApp];
//    int idx=suitIdx%app.itemList.count;
    NSString *strID;
    if (self.tagType==1) {
        strID=[[app.itemList objectAtIndex:suitIdx] objectForKey:@"colorSn"] ;
    }
    else{
        strID=[[app.itemList objectAtIndex:suitIdx] objectForKey:@"itemSn"] ;
    }
  
//    _suitIdx=idx;
    proId=strID;
    [self onSegmentClick:self.navigationItem.titleView];
}
-(void)setProductId:(NSString *)productId{
    proId=productId;
     [self onSegmentClick:self.navigationItem.titleView];
//    InfoUIM *info=[[InfoUIM alloc] init];
//    info.view.frame=CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-230);
////    [self presentViewController:info animated:YES completion:nil];
//    [self.view addSubview:info.view];
//    [self addChildViewController:info];
//    [self ShowChild:info.view];
//    UISegmentedControl *seg=(id)self.navigationItem.titleView;
//    if (seg.selectedSegmentIndex==0) {
//        NSDictionary *suit=[self suitDict];
//        NSString *itemId=suit[@"item_id"];
//#warning 调用数据库
//        NSArray *pdts=[AppUI getProductBySuit:itemId];
//        NSMutableArray *items=[NSMutableArray array];
//        for (NSString *pdt in pdts) {
//#warning 调用数据库
//            NSArray *skus=[AppUI getProductSku:pdt];
//            NSDictionary *sku=[skus firstObject];
//            if (sku) {
//                [items addObject:sku[@"sku_id"]];
//            }
//        }
//        info.items=items;
//    }
//    if (seg.selectedSegmentIndex==1) {
//        info.items=@[productId];
//    }
//    info.itemId=productId;
}
-(void)ShowChild:(UIView *)view{
    [UIView animateWithDuration:0.3 animations:^{
        view.frame=CGRectMake(0, 230,view.frame.size.width, view.frame.size.height);
        
    } completion:^(BOOL finished) {
        
    }];
}
-(NSDictionary*)suitDict{
    
    MainApp *app=[MainApp mainApp];
    NSString *itemId=[app.itemList objectAtIndex:self.suitIdx];
#warning 调用数据库
    return [AppUI getSuitDict:itemId];
}
-(void)suitDict:(void (^) (NSDictionary *dicData))success{
    
    [responseCtr getGoodsInfo:self.tagType ColorSn:proId Success:^(NSDictionary *arrReceiveData) {
        success(arrReceiveData);
    } Failure:^(NSError *error) {
        
    }];
}
-(void)GoodsDict:(void (^) (NSDictionary *dicData))success{
    [responseCtr SearchTagType:1 CurrentPage:1 PageSize:20 CatID:_catId SearchKey:@"" Success:^(NSDictionary *dicReceiveData) {
         success(dicReceiveData);
    } Failure:^(NSError *error) {
        
    }];
}
@end
